package com.zhuiyun.project.api.sysarticle.service.impl;

import com.zhuiyun.project.api.article.mapper.PublishArticleMapper;
import com.zhuiyun.project.api.friend.model.FriendModel;
import com.zhuiyun.project.api.joingroup.model.JoinGroupModel;
import com.zhuiyun.project.api.sysarticle.mapper.ArticleMapper;
import com.zhuiyun.project.api.sysarticle.model.ArticleModel;
import com.zhuiyun.project.api.sysarticle.model.CommentModel;
import com.zhuiyun.project.api.sysarticle.service.ArticleService;
import com.zhuiyun.project.util.RedisUtil;
import com.zhuiyun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import com.zhuiyun.project.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @ClassName ArticleServiceImpl
 * @Description TODO
 * @Author 韩佳轩
 * @Date 2023/6/15 10:34
 **/
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    PublishArticleMapper publishArticleMapper;
    public static final int expirationTimeSeconds = 2;
    String swearing="傻逼,操你妈,垃圾,傻缺";
    /**
     * @Author 韩佳轩
     * @Description 小程序首页
     * @Date 2023/6/15 16:49
     * @Return com.zhuiyun.project.util.pageUtil.PageResult
     * @param articleModel
     * @param
     */
    @Override
    public List<ArticleModel> getArticleAll(ArticleModel articleModel) {
        List<ArticleModel> articleList;
        // 尝试从 Redis 中获取文章列表
        Object articleAll1 = RedisUtil.get("articleAll");
        if (articleAll1 instanceof List) {
            articleList = (List<ArticleModel>) articleAll1;
        } else {
            // Redis 中没有缓存，从数据库中获取文章列表
            articleList = articleMapper.getArticleAll(articleModel);
            // 将文章列表存入 Redis 缓存，并设置过期时间为一分钟
            if (!articleList.isEmpty()) {
                RedisUtil.set("articleAll", articleList, expirationTimeSeconds);
            }
        }
        return articleList;
    }
    /**
     * @Author 小江
     * @Description //TODO 根据病种查询
     * @Date 20:13 2023/7/11
     **/
    @Override
    public PageResult ArticleLater(ArticleModel articleModel, PageRequest pageRequest) {
        List<ArticleModel> articleModels;
        String key = "articleModels" + articleModel.getLabelId();
        // 尝试从缓存中获取文章列表
        Object cachedArticleModels = RedisUtil.get(key);
        if (cachedArticleModels instanceof List) {
            // 如果缓存中有数据，则直接使用缓存中的文章列表
            articleModels = (List<ArticleModel>) cachedArticleModels;
        } else {
            // 如果缓存中没有数据，则从数据库中获取文章列表
            articleModels = articleMapper.ArticleLater(articleModel);
            // 将获取到的文章列表存入缓存
            boolean cacheResult = RedisUtil.set(key, articleModels, expirationTimeSeconds);
            if (!cacheResult) {
                // 如果存入缓存失败，可以根据实际情况处理，比如记录日志等
                // ...
            }
        }
        // 使用分页插件进行分页
        MybatisPageHelper.startPage(pageRequest);
        return MybatisPageHelper.getPageResult(articleModels);
    }

    /**
     * @Author 韩佳轩
     * @Description sys全查文章
     * @Date 2023/6/15 16:49
     * @Return com.zhuiyun.project.util.pageUtil.PageResult
     * @param articleModel
     * @param pageRequest
     */
    @Override
    public PageResult getSysArticleAll(ArticleModel articleModel, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(articleMapper.getSysArticleAll(articleModel));
        return pageResult;
    }
    /**
     * @Author 韩佳轩
     * @Description 下架文章
     * @Date 2023/6/15 16:50
     * @Return int
     * @param id
     */
    @Override
    public int deleteArticle(int id) {
        return articleMapper.deleteArticle(id);
    }
    /**
     * @Author 韩佳轩
     * @Description 小程序首页跳转详情
     * @Date 2023/6/20 16:25
     * @Return
     * @param
     */
    @Override
    public List<ArticleModel> ArticleMain(ArticleModel articleModel) {
        return articleMapper.ArticleMain(articleModel);
    }
    /**
     * @Author 韩佳轩
     * @Description 医师列表
     * @Date 2023/6/20 16:25
     * @Return
     * @param
     */
    @Override
    public List<ArticleModel> ArticleDoctorList(ArticleModel articleModel) {
        ArticleModel articleModel1 = new ArticleModel();
        Integer userId = articleModel.getUserId();
        int i = publishArticleMapper.selectDoctorId(userId);
        articleModel1.setUserId(i);
        return articleMapper.ArticleDoctorList(articleModel1);
    }
    /**
     * @Author 韩佳轩
     * @Description 评论全查
     * @Date 2023/6/22 10:43
     * @Return java.util.List<com.zhuiyun.project.api.sysarticle.model.CommentModel>
     * @param commentModel
     */
    @Override
    public List<CommentModel> CommentAll(CommentModel commentModel) {

        return articleMapper.CommentAll(commentModel);
    }
    /**
     * @Author 韩佳轩
     * @Description 添加评论
     * @Date 2023/6/22 10:48
     * @Return int
     * @param
     */
    @Override
    public int insertComment(CommentModel commentModel) {
        Date date=new Date();
        commentModel.setCreateTime(date);
        //获取评论的文本内容
        String commentContent = commentModel.getCommentContent();
        //将字符串分割逗号
        String[] sw = swearing.split(",");
        for (int i=0;i<sw.length;i++){
            //将脏话替换**
            commentContent = commentContent.replaceAll(sw[i], "**");
        }
        //获取评论内容
        commentModel.setCommentContent(commentContent);
        return articleMapper.insertComment(commentModel);
    }
    /**
     * @Author 韩佳轩
     * @Description 权限私密 （公开默认全查）
     * @Date 2023/6/30 9:03
     * @Return
     * @param articleModel
     */
    @Override
    public int Privacy(ArticleModel articleModel) {
        return articleMapper.Privacy(articleModel);
    }
/**
     * @Author 韩佳轩
     * @Description 转发文章的好友列表
     * @Date 2023/7/14 11:19
     * @Return
     * @param friendmodel
     */
    @Override
    public List<FriendModel> FriendList(FriendModel friendmodel) {

        return articleMapper.FriendList(friendmodel);
    }
    /**
     * @Author 韩佳轩
     * @Description  转发文章的群聊列表
     * @Date 2023/7/31 16:54
     * @Return java.util.List<com.zhuiyun.project.api.friend.model.FriendModel>
     * @param  joinGroupModel
     */
    @Override
    public List<JoinGroupModel> GroupList(JoinGroupModel joinGroupModel) {
        return articleMapper.GroupList(joinGroupModel);
    }

}
